<?php
namespace Core\Controller;

use Core\Exception\BusinessException;
use Core\Service\Authenticate\IdentityManagerInterface;
use Core\Session\AdminSessionHelper;
use Core\Session\ApplicationSessionHelper;
use Core\Session\FrontSessionHelper;
use Model\TableFinder;
use Model\User;
use Zend\Mvc\MvcEvent;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;

class FrontBaseController extends  BaseController{

    /**
     * @var User
     */
    protected $_user;

    /**
     * @var IdentityManagerInterface;
     */
    protected $identityManager;

    /**
     * @var \Zend\View\Model\ViewModel
     */
    protected $viewModel;



    public function __construct(IdentityManagerInterface $identityManager)
    {
        $this->identityManager = $identityManager;
        $this->viewModel = new ViewModel();
    }

    /**
     * @return \Zend\View\Model\ViewModel
     */
    public function getViewModel()
    {
        return $this->viewModel;
    }


    public function onDispatch(MvcEvent $e)
    {
        $this->setUser(FrontSessionHelper::getUser());
        $controller = $this->getEvent()->getRouteMatch()->getParam('controller');
        $action = $this->getEvent()->getRouteMatch()->getParam('action');
        ApplicationSessionHelper::setCurrentModule('front');
        ApplicationSessionHelper::setCurrentController($controller);
        ApplicationSessionHelper::setCurrentAction($action);
        return parent::onDispatch($e);
    }

    public function initPageWithLayout(){
        $pageId = $this->params()->fromRoute('pageId');
        $pageTable = $this->getTableFinder()->getPageTable();
        $page = $pageTable->getPage($pageId);
        $template = $page->getTemplate();
        $this->layout($template->file_name.'.php');
        $this->layout()->setVariable('templateItem',$template);
        $this->getViewModel()->setVariable('pageItem',$page);
    }





    /**
     * @param \Core\Service\Authenticate\IdentityManagerInterface $identityManager
     */
    public function setIdentityManager($identityManager)
    {
        $this->identityManager = $identityManager;
    }

    /**
     * @return \Core\Service\Authenticate\IdentityManagerInterface
     */
    public function getIdentityManager()
    {
        return $this->identityManager;
    }


    /**
     * @param \Model\User $user
     */
    public function setUser($user)
    {
        $this->_user = $user;
    }

    /**
     * @return \Model\User
     */
    public function getUser()
    {
        return $this->_user;
    }

    protected function genJsonErrorView(\Exception $ex){
        if($ex instanceof BusinessException){
            $msg = $ex->getMessage();
        }else{
            $msg = 'Lỗi hệ thống. Vui lòng liên hệ chúng tôi để khắc phục.';
        }
        return new  JsonModel(array('success'=>false, 'msg'=>$msg));
    }


    protected function genErrorView(\Exception $ex){
        if($ex instanceof BusinessException){
            $msg = $ex->getMessage();
        }else{
            $msg = 'Lỗi hệ thống. Vui lòng liên hệ chúng tôi để khắc phục.';
        }
        $viewModel = new  ViewModel(array('error_msg' => $msg));
        $viewModel->setTerminal(true);
        return $viewModel;
    }



}
